package com.valuedaas.schoolsafety.common.config;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.util.UriComponentsBuilder;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.paths.AbstractPathProvider;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.net.InetAddress;
import java.net.UnknownHostException;

import static springfox.documentation.spring.web.paths.Paths.removeAdjacentForwardSlashes;

/**
 * 接口文档自动生成 swagger 配置
 */

@Profile("!prod")
@Configuration
@EnableSwagger2
@ComponentScan(basePackages = {
        "com.valuedaas.schoolsafety.controller"})
public class Swagger2Config {

    @Value("${server.port}")
    private String port;

    @Value("${spring.profiles.active}")
    private String active;

    @Bean
    public Docket createRestApi() {
        Docket docket = null;
        if (StringUtils.isNotEmpty(active) && active.equals("dev")) {
            docket = new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).useDefaultResponseMessages(false)
                    .pathProvider(new CustRelativePathProvider()).select()
                    .apis(RequestHandlerSelectors.basePackage("com.valuedaas.schoolsafety")).paths(PathSelectors.any())
                    .build();
            ;
        } else {
            docket = new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())/*.host("school.valuedaas.com")*/
                    .useDefaultResponseMessages(false).pathProvider(new CustRelativePathProvider()).select()
                    .apis(RequestHandlerSelectors.basePackage("com.valuedaas.schoolsafety")).paths(PathSelectors.any())
                    .build();
            ;
        }
        return docket;
    }

    private class CustRelativePathProvider extends AbstractPathProvider {
        public static final String ROOT = "/";

        @Override
        public String getOperationPath(String operationPath) {
            UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromPath("/");
            String uri = removeAdjacentForwardSlashes(uriComponentsBuilder.path(operationPath).build().toString());
            return uri + ".do";
        }

        @Override
        protected String applicationPath() {
            //return isNullOrEmpty(contextPath) ? ROOT : contextPath;
            return ROOT;
        }

        @Override
        protected String getDocumentationPath() {
            return ROOT;
        }
    }


    private ApiInfo apiInfo() {
        String host = getHost();
        return new ApiInfoBuilder().title("校园安全-后端API文档")
                .description("API根地址：http://" + host + ":" + port + "/swagger-ui.html")
                .termsOfServiceUrl("http://" + host + ":" + port)
                .contact(new Contact("万缕数据", "www.valuedaas.com", "chenhua@valuedaas.com")).version("v1").build();
    }

    private String getHost() {
        try {
//		    if(ProfileUtil.getActiveProfile().equals("dev"))
//                return "localhost";
            return InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
        }
        return "localhost";
    }

}